package cn.sskxyz.mongodb.compress;

import java.util.List;

import org.bson.Document;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.MongoConverter;

import com.mongodb.client.MongoCollection;

public class MongoCompressTemplate extends MongoTemplate {

	private List<CompressKey> compressKeys;

	private CompressProcessor compressProcessor = new CompressProcessor();

	public MongoCompressTemplate(MongoDatabaseFactory mongoDbFactory, MongoConverter mongoConverter,
			List<CompressKey> compressKeys) {
		super(mongoDbFactory, mongoConverter);
		this.compressKeys = compressKeys;
	}

	@Override
	protected MongoCollection<Document> prepareCollection(MongoCollection<Document> collection) {
		MongoCollection<Document> originCollection = super.prepareCollection(collection);
		return new CompressMongoCollection<>(originCollection, this.compressKeys, compressProcessor);
	}

	public void setCompressProcessor(CompressProcessor compressProcessor) {
		this.compressProcessor = compressProcessor;
	}

}
